package com.wish.g1.house.controller;


import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g1.common.core.utils.poi.ExcelUtil;
import com.wish.g1.common.core.web.controller.BaseController;
import com.wish.g1.common.core.web.domain.AjaxResult;
import com.wish.g1.common.core.web.page.TableDataInfo;
import com.wish.g1.common.log.enums.BusinessType;
import com.wish.g1.common.security.annotation.RequiresPermissions;
import com.wish.g1.common.log.annotation.Log;
import com.wish.g1.house.entity.House;
import com.wish.g1.house.service.IHouseService;
import com.wish.g1.system.api.HousingFegin;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 房源表 前端控制器
 * </p>
 *
 * @author mayingchao
 * @since 2022-11-18
 */
@Slf4j
@RestController
@RequestMapping("/house")
public class HouseController extends BaseController {


    @Autowired
    private IHouseService houseService;

    @Autowired
    private HousingFegin housingFegin;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    /**
     * 查询房源列表
     */
    // @RequiresPermissions("house:house:list")
    @GetMapping("/list")
    public TableDataInfo list(House house)
    {
        startPage();
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.setEntity(house);
        List<House> list = houseService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * fegin调用 楼盘列表
     */
    // @RequiresPermissions("house:house:selHousing")
    @PostMapping("/selHousing")
    public TableDataInfo selHousing(){
        TableDataInfo list = housingFegin.list();
        return getDataTable(list.getRows());
    }

    /**
     * 导出房源列表
     */
    // @RequiresPermissions("house:house:export")
    @Log(title = "房源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, House house)
    {
        List<House> list = houseService.selectHouseList(house);
        ExcelUtil<House> util = new ExcelUtil<House>(House.class);
        util.exportExcel(response, list, "房源数据");
    }

    /**
     * 获取房源详细信息
     */
    // @RequiresPermissions("house:house:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(houseService.getById(id));
    }

    /**
     * 新增房源
     */
    // @RequiresPermissions("house:house:add")
    @Log(title = "房源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody House house)
    {
        boolean b = houseService.save(house);
        if (b){
            IndexQuery indexQuery = new IndexQueryBuilder()
                    .withId(house.getId().toString())
                    .withObject(house)
                    .build();
            elasticsearchRestTemplate.save(house,indexQuery);

        }
        return toAjax(b);
    }

    /**
     * 修改房源
     */
    // @RequiresPermissions("house:house:edit")
    @Log(title = "房源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody House house)
    {
        return toAjax(houseService.updateHouse(house));
    }

    /**
     * 删除房源
     */
    // @RequiresPermissions("house:house:remove")
    @Log(title = "房源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(houseService.deleteHouseByIds(ids));
    }


    @GetMapping("/bulkIndex")
    public void bulkIndex(){
        log.info("-----定时任务同步es数据");
        List<House> list = houseService.list();
        List<IndexQuery> indexQueryList = new ArrayList<>();
        for (House house : list) {
            IndexQuery indexQuery = new IndexQueryBuilder()
                    .withId(house.getId().toString())
                    .withObject(house)
                    .build();
            indexQueryList.add(indexQuery);
        }
        elasticsearchRestTemplate.bulkIndex(indexQueryList,House.class);
    }

    @GetMapping("/queryForList")
    public SearchHits<House> queryForList(){
        MatchAllQueryBuilder allQuery = QueryBuilders.matchAllQuery();
        NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(allQuery);
        SearchHits<House> search = elasticsearchRestTemplate.search(nativeSearchQuery, House.class);
        for (SearchHit<House> houseSearchHit : search) {
            System.out.println(houseSearchHit);
        }
        return search;
    }


}
